﻿using Microsoft.AspNetCore.Http;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace GatewayServerService.Data
{
    public class DBIotDeviceTaskTimeInterval
    {



        public iot_device_task_time_interval getModel(string guid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            return ef.iot_device_task_time_interval.Where(c => c.ti_guid == guid).FirstOrDefault();
        }



        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_task_time_interval req)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            //先查一下有没重复提交
            iot_device_task_time_interval model = ef.iot_device_task_time_interval.Where(c => c.ti_guid == req.ti_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.ti_add_time = DateTime.Now;
                req.ti_mod_time = DateTime.Now;
                req.ti_start_time = DateTime.Now;
                req.ti_interval_name = "秒";
                req.ti_interval_unit = "ss";
                // req.ti_complete = 0;
                // req.ti_execute_count = 0;
                req.ti_c_guid = LoginUserInfo.userCompanyGuid;
                ef.iot_device_task_time_interval.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.ti_title = req.ti_title;
                model.ti_status = req.ti_status;
                model.ti_interval_value = req.ti_interval_value;
                model.ti_key = req.ti_key;
                model.ti_task_guid = req.ti_task_guid;
                //model.ti_repeat_count = req.ti_repeat_count;
                model.ti_mod_time = DateTime.Now;

                update(model);
            }

            return true;
        }
        static object updobj = new object();
        public void update(iot_device_task_time_interval model)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            lock (updobj)
            {
                ef.iot_device_task_time_interval.Update(model);
                ef.SaveChanges();
            }

        }
    }
}
